package com.nf.xiaomi.dao.impl;

import com.nf.dbutils.handlers.BeanHandler;
import com.nf.xiaomi.dao.ProductInfoDao;
import com.nf.dbutils.SqlExecutor;
import com.nf.dbutils.handlers.BeanListHandler;
import com.nf.xiaomi.entity.ProductInfoEntity;
import com.nf.xiaomi.entity.ProductEntity;
import com.nf.xiaomi.util.SqlExecutorUtils;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ProductInfoDaoImpl implements ProductInfoDao {
    SqlExecutor executor = SqlExecutorUtils.getExecutor();
    @Override
    public List<ProductInfoEntity> getAttribute(int pid, String memory) {
        String sql="SELECT a.attribute_id,p.product_detail_price,p.product_detail_amount,a.attribute_list from attribute a " +
                "JOIN product_detail p on a.attribute_id=p.attribute_id WHERE p.product_id=? and attribute_list like ?";
        return executor.query(sql,new BeanListHandler<>(ProductInfoEntity.class),pid,memory);
    }

    @Override
    public ProductEntity getProductInfo(int pid, String memory, String color) {
        String sql="SELECT product_detail_id,product_detail_price,t.nstate FROM product_detail t JOIN attribute " +
                "a on t.attribute_id=a.attribute_id WHERE product_id=? " +
                "AND attribute_list ->> '$.color' = ? " +
                "AND  attribute_list ->>'$.memory' = ? ";
        return executor.query(sql,new BeanHandler<>(ProductEntity.class),pid,color,memory);
    }

    @Override
    public List<ProductEntity> getAttributeList(Object... params) {
        if(params.length<=0){
            return new ArrayList<ProductEntity>();
        }
        String sql="SELECT product_detail_id,product_detail_price,attribute_list from product_detail t " +
                "JOIN attribute a ON t.attribute_id = a.attribute_id  where product_detail_id in(";

        for (int i = 0; i < params.length; i++) {
            sql+="?";
            if(i!=params.length-1){
                sql+=",";
            }
        }
        sql+=")";
        return executor.query(sql,new BeanListHandler<>(ProductEntity.class),params);
    }

    @Override
    public List<ProductEntity> getProductInfoList(int productID) {
        String sql ="SELECT product_detail_id,product_detail_picture,product_detail_price," +
                "product_detail_amount,attribute_list,t.nstate from product_detail" +
                " t JOIN attribute a on t.attribute_id=a.attribute_id " +
                "where product_id=?";

        return executor.query(sql,new BeanListHandler<>(ProductEntity.class),productID);
    }

    @Override
    public Integer insertProductInfos(Integer productID, List<ProductInfoEntity> productInfoList) {
        String value="(?,?,?,?,?,?)";
        String sql="insert into product_detail(product_id,attribute_id,product_detail_price," +
                "product_detail_amount,product_detail_picture,nstate) values";
        for (int i = 0; i < productInfoList.size(); i++) {
            sql+=value;
            if(i!=productInfoList.size()-1){
                sql+=",";
            }
        }
        Object[] params = handParams(productID, productInfoList);
        return executor.update(sql,params);
    }

    private Object[] handParams(Integer productID,List<ProductInfoEntity> productInfoList){
        Object[] params=new Object[productInfoList.size()*6];

        for (int i = 0; i < productInfoList.size(); i++) {
            int index=(i)*6;
            params[index]=productID;
            params[index+1]=productInfoList.get(i).getId();
            params[index+2]=productInfoList.get(i).getPrice();
            params[index+3]=productInfoList.get(i).getAmount();
            params[index+4]=productInfoList.get(i).getPicture();
            params[index+5]=1;
        }
        return params;
    }
}
